package com.key.win.business.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.key.win.base.page.MybatisPageServiceTemplate;
import com.key.win.base.web.PageRequest;
import com.key.win.base.web.PageResult;
import com.key.win.business.chat.dao.ImApplyDetailDao;
import com.key.win.business.chat.model.ImApplyDetail;
import com.key.win.business.chat.service.ImApplyDetailService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImApplyDetailServiceImpl extends ServiceImpl<ImApplyDetailDao, ImApplyDetail> implements ImApplyDetailService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public PageResult<ImApplyDetail> findApplyDetailByPaged(PageRequest<ImApplyDetail> t) {
        MybatisPageServiceTemplate<ImApplyDetail, ImApplyDetail> query = new MybatisPageServiceTemplate<ImApplyDetail, ImApplyDetail>(this.baseMapper) {
            @Override
            protected AbstractWrapper constructWrapper(ImApplyDetail Apply) {
                LambdaQueryWrapper<ImApplyDetail> lambdaQueryWrapper = buildWrapper(Apply);
                return lambdaQueryWrapper;
            }
        };
        return query.doPagingQuery(t);
    }

    private LambdaQueryWrapper<ImApplyDetail> buildWrapper(ImApplyDetail imApplyDetail) {
        if (imApplyDetail == null) {
            logger.error("ImApplyDetail is null!!");
            throw new IllegalArgumentException("查询条件为空！");
        }
        if (StringUtils.isBlank(imApplyDetail.getUserId())) {
            logger.error("ImApplyDetail userId is null!!");
            throw new IllegalArgumentException("申请人Id为空！");
        }
        LambdaQueryWrapper<ImApplyDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ImApplyDetail::getUserId, imApplyDetail.getUserId());
        if (StringUtils.isNotBlank(imApplyDetail.getApplyId())) {
            lambdaQueryWrapper.like(ImApplyDetail::getApplyId, imApplyDetail.getApplyId());
        }
        if (StringUtils.isNotBlank(imApplyDetail.getDesc())) {
            lambdaQueryWrapper.like(ImApplyDetail::getDesc, imApplyDetail.getDesc());
        }
        return lambdaQueryWrapper;
    }

    public List<ImApplyDetail> findApplyDetail(ImApplyDetail ImApplyDetail) {
        return this.list(this.buildWrapper(ImApplyDetail));
    }

    public boolean addApplyDetail(ImApplyDetail imApplyDetail) {
        if (imApplyDetail == null) {
            logger.error("ImApplyDetail is null!!");
            throw new IllegalArgumentException("消息为空！");
        }
        if (StringUtils.isBlank(imApplyDetail.getUserId())) {
            logger.error("ImApplyDetail userId is null!!");
            throw new IllegalArgumentException("申请人为空！");
        }
        return this.save(imApplyDetail);
    }

    @Override
    public boolean deleteApplyDetailByApplyId(String applyId) {
        LambdaQueryWrapper<ImApplyDetail> query = new LambdaQueryWrapper<>();
        query.eq(ImApplyDetail::getApplyId,applyId);
        return this.remove(query);
    }

    public List<ImApplyDetail> getImApplyDetailByApplyId(String applyId) {
        LambdaQueryWrapper<ImApplyDetail> query = new LambdaQueryWrapper<>();
        query.eq(ImApplyDetail::getApplyId,applyId);
        return this.list(query);
    }
}
